#!/usr/bin/python3
"""
    ----coding:utf-8----
    @Author   : sobo
    @Time     : 2018/3/20 下午11:50
    @Software : 
    @File     : anthentication.py

"""
from django.contrib.auth.models import User


class EmailAuthBackend(object):
    """
    使用电子邮件帐户进行身份验证。
    """

    def authenticate(self, username=None, password=None):
        try:
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
